package com.itboye.jinpuyiyuan.activity.home.testserver;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.adapter.LeftAdapter;
import com.itboye.jinpuyiyuan.adapter.ShopCartAdapter;
import com.itboye.jinpuyiyuan.adapter.TestServerFixedAdapter;
import com.itboye.jinpuyiyuan.adapter.TestServerOptionLeftAdapter;
import com.itboye.jinpuyiyuan.adapter.TestServerOptionRightAdapter;
import com.itboye.jinpuyiyuan.adapter.TestServerOrderAdapter;
import com.itboye.jinpuyiyuan.adapter.TestServerPayGridAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.RegisterHospitalBean;
import com.itboye.jinpuyiyuan.bean.TestServerFixedBean;
import com.itboye.jinpuyiyuan.bean.TestServerOptionBean;
import com.itboye.jinpuyiyuan.bean.TestServerOrder;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/11 0011.
 */

public class TestServerActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;

    //预约的界面ui
    private RelativeLayout testTablePayRL;
    private TextView testTablePayTv,testTablePresentationTv,testTableOrderTv;
    private ImageView moveLine;
    private LinearLayout testServerOrderLL,testServerPayLL,testServerPresentLL;
    private ListView testOrderList;
    private TestServerOrderAdapter testServerOrderAdapter;
    private ArrayList<TestServerOrder> list;
    //moveline
    private int moveDistance;//移动的距离
    private int moveLineWidth;
    private int currIndex;

   //购买的界面ui
    private GridView testServerPayGv;
    private LinearLayout testServerTableLL;//有全部的table
    private TextView testServerPayAllTv,testServerPayTable1,testServerPayMoreTV;
    private ListView testServerPayFixedList;//固定体检套餐list
    private LinearLayout testServerPayOptionLL;//包含自选list的ll，控制显示
    private ListView testServerPayOptionLeftList,testServerPayOptionRightList;//自选体检套餐list
    private TextView testServerPayFixedTv,testServerPayOptionTv;//固定体检套餐和自选体检套餐



    private TestServerPayGridAdapter testServerPayGridAdapter;
    private ArrayList<String> gridList;
    private ArrayList<String> tableList;//table显示的list

    private RelativeLayout paygoodsRl;//购买的layout
    private TextView confirmPayTv;//确认购买按钮
    private RelativeLayout goodsCartList;//购物车 购物列表
    private RelativeLayout shopcartRl;//包含的购物车图片
    private ImageView payGoodsImg;
    private TextView payManeyTv,goodsNumTv;

    private ArrayList<TestServerFixedBean> fixedList;//固定体检套餐的list
    private TestServerFixedAdapter fixedAdapter;//固定体检套餐的adapter
    private ListView goodCartNumLv;//购物list
    private TextView deleteALlTv;//清除所有tv
    private ImageView deleteAllImg;//清除所有图片

    private ArrayList<TestServerOptionBean> optionLeftList;//自选体检套餐left的list
    private ArrayList<TestServerOptionBean.Child> rightList;//自选体检套餐right的list

    private TestServerOptionLeftAdapter leftAdapter;

    private TestServerOptionRightAdapter rightAdapter;
    private ShopCartAdapter shopCartAdapter;




    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_testserver);
        initTitleView();
        initPayData();

    }
    //购买相关数据
    private void initPayData() {
        gridList=new ArrayList<>();
        tableList=new ArrayList<>();
        for (int i=0;i<13;i++){
         gridList.add("金色童年"+i);
         if (i==12){
             gridList.add("返回");
         }
        }
        testServerPayGridAdapter=new TestServerPayGridAdapter(this,gridList);
        testServerPayGv.setAdapter(testServerPayGridAdapter);
        testServerPayTable1.setText(gridList.get(0));
        testServerPayGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position==(gridList.size()-1)){

                }else {
                    testServerPayTable1.setText(gridList.get(position));
                    testServerPayAllTv.setTextColor(getResources().getColor(R.color.black));
                    testServerPayTable1.setTextColor(getResources().getColor(R.color.title_blue));
                }

                testServerPayGv.setVisibility(View.GONE);
                testServerTableLL.setVisibility(View.VISIBLE);


            }
        });
        //固定体检套餐数据初始化
        fixedList=new ArrayList<>();
        for (int j=0;j<6;j++){
            TestServerFixedBean testServerFixedBean = new TestServerFixedBean();
            testServerFixedBean.setLogoUrl("https://img02.sogoucdn.com/net/a/04/link?url=http%3A%2F%2Fimg04.sogoucdn.com%2Fapp%2Fa%2F100520024%2Fd9ddcc894b106620d560b22cf890a2fc&appid=122");
            testServerFixedBean.setNewPrice(325);
            testServerFixedBean.setOldPrice(425);
            testServerFixedBean.setTitle("家和万事兴，敬老金普行");
            testServerFixedBean.setStrength("内科，外科，眼科(眼底，裂缝灯)");
            fixedList.add(testServerFixedBean);
        }
        fixedAdapter=new TestServerFixedAdapter(this,fixedList);
        testServerPayFixedList.setAdapter(fixedAdapter);

        testServerPayFixedList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                view.findViewById(R.id.queryDetail).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(TestServerActivity.this,TestServerFixedQueryDetailActivity.class));
                    }
                });


            }
        });

        //自选体检套餐数据初始化
        optionLeftList=new ArrayList<>();
        rightList=new ArrayList<>();
        for (int k=0;k<5;k++){

            TestServerOptionBean testServerOptionBean = new TestServerOptionBean();
            testServerOptionBean.setDepartment("外科"+k);
            ArrayList<TestServerOptionBean.Child> optionRightList = new ArrayList<>();//自选体检套餐right的list
            for (int e=0;e<5;e++){
                TestServerOptionBean.Child child = new TestServerOptionBean.Child();
                child.setName("甲状腺"+k+""+e);
                child.setPrice(256);
                child.setPayNum(0);
                optionRightList.add(child);
            }
            testServerOptionBean.setList(optionRightList);
            optionLeftList.add(testServerOptionBean);
        }
        leftAdapter=new TestServerOptionLeftAdapter(this,optionLeftList);
        //
        rightList=optionLeftList.get(0).getList();

        rightAdapter=new TestServerOptionRightAdapter(this,optionLeftList.get(0).getList(),"0");
        shopCartAdapter=new ShopCartAdapter(this,optionLeftList.get(0).getList());

        testServerPayOptionLeftList.setAdapter(leftAdapter);
        testServerPayOptionRightList.setAdapter(rightAdapter);
        goodCartNumLv.setAdapter(shopCartAdapter);
        testServerPayOptionLeftList.setOnItemClickListener(this);
        testServerPayOptionRightList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;
            //购买
            case R.id.testTablePayRL:
            case R.id.testTablePayTv:
                cancleTableState() ;
                testServerPayLL.setVisibility(View.VISIBLE);

                tranSlate(-1,300);
                testTablePayTv.setTextColor(getResources().getColor(R.color.title_blue));
                currIndex=-1;

                break;
                //预约
            case R.id.testTableOrderTv:
                cancleTableState() ;
                testServerOrderLL.setVisibility(View.VISIBLE);
                tranSlate(0,300);
                testTableOrderTv.setTextColor(getResources().getColor(R.color.title_blue));
                currIndex=0;
                break;

                //报告
            case R.id.testTablePresentationTv:
                cancleTableState() ;
                testServerPresentLL.setVisibility(View.VISIBLE);
                tranSlate(1,300);
                testTablePresentationTv.setTextColor(getResources().getColor(R.color.title_blue));
                currIndex=1;
                break;

            //购买界面
            //全部
            case R.id.testServerPayAllTv:
                testServerPayTable1.setTextColor(getResources().getColor(R.color.black));
                testServerPayAllTv.setTextColor(getResources().getColor(R.color.title_blue));

                break;
            // 更多
            case R.id.testServerPayMoreTV:
                testServerTableLL.setVisibility(View.GONE);
                testServerPayGv.setVisibility(View.VISIBLE);
                break;

            //固定体检套餐
            case R.id.testServerPayFixedTv:
                canclePayState();
                testServerPayFixedList.setVisibility(View.VISIBLE);
                testServerPayFixedTv.setTextColor(getResources().getColor(R.color.title_blue));
                break;
            //自选体检套餐
            case R.id.testServerPayOptionTv:
                canclePayState();
                testServerPayOptionLL.setVisibility(View.VISIBLE);
                testServerPayOptionTv.setTextColor(getResources().getColor(R.color.title_blue));

                paygoodsRl.setVisibility(View.VISIBLE);

                break;
            case R.id.shopcartRl:
            case R.id.payGoodsImg:
            case R.id.goodsNumTv:
//            goodsCartList.setVisibility(View.VISIBLE);

                break;

        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        moveDistance = testTablePayRL.getMeasuredWidth();
        moveLineWidth=testTablePayTv.getMeasuredWidth();
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(moveLineWidth,4);

        params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.TableLL);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL,R.id.TableLL);
        moveLine.setLayoutParams(params);

        Log.d("==========",moveDistance+"");
    }

    private void initTitleView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.title_blue));
        }
        leftTv.setVisibility(View.GONE);
        rightImg.setVisibility(View.GONE);
        tvTitle.setText("体检服务");

        //预约相关数据
        testTableOrderTv.setTextColor(getResources().getColor(R.color.title_blue));
        currIndex = 0;
        testTableOrderTv.setTextColor(getResources().getColor(R.color.title_blue));

        list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {

            TestServerOrder testServerOrder = new TestServerOrder();
            testServerOrder.setOrderDate("2017.12.6");
            testServerOrder.setOrderPersonNum(40);
            testServerOrder.setOrderState("不可预约");
            testServerOrder.setOrderWeekday("周三");
            list.add(testServerOrder);
        }
        testServerOrderAdapter=new TestServerOrderAdapter(this,list);
        testOrderList.setAdapter(testServerOrderAdapter);





    }
    /**
     * 平移动画
     *
     * @param currentItem
     * @param durationMillis
     */
    public void tranSlate(int currentItem, long durationMillis) {
        float one = moveDistance;
        Log.v("offest", "" + moveDistance);

        Animation animation = new TranslateAnimation(currIndex * one,
                currentItem * one, 0, 0);// 平移动画
        Log.v("currentoffest", "" + currIndex * one);
        Log.v("currentItem", "" + currentItem * one);
        animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
        animation.setDuration(durationMillis);// 动画持续时间0.2秒
        moveLine.startAnimation(animation);// 是用ImageView来显示动画的
    }

    //购买 预约 报告
    public void cancleTableState(){
        testTablePayTv.setTextColor(getResources().getColor(R.color.black));
        testTableOrderTv.setTextColor(getResources().getColor(R.color.black));
        testTablePresentationTv.setTextColor(getResources().getColor(R.color.black));
        testServerOrderLL.setVisibility(View.GONE);
        testServerPayLL.setVisibility(View.GONE);
        testServerPresentLL.setVisibility(View.GONE);
    }
    //固定体检套餐和自选体检套餐

    public void canclePayState(){
        testServerPayFixedTv.setTextColor(getResources().getColor(R.color.black));
        testServerPayOptionTv.setTextColor(getResources().getColor(R.color.black));

        testServerPayFixedList.setVisibility(View.GONE);
        testServerPayOptionLL.setVisibility(View.GONE);
        paygoodsRl.setVisibility(View.GONE);
    }
//TestServerOptionLeftList item点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.d("===onItemClick===",optionLeftList.get(position).toString());
        leftAdapter.cancleAllItemState(position);
//        leftAdapter.notifyDataSetChanged();
        rightList=optionLeftList.get(position).getList();
        rightAdapter.addData(optionLeftList.get(position).getList(),""+position);

//                Log.d("======",list.get(position).getList().toString());

    }



}
